package cn.bluethink.mouse;

import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JOptionPane;

import cn.bluethink.MapFrame;
import cn.bluethink.graphical.Polygon;
import cn.bluethink.utile.GraphicalUtil;
import cn.bluethink.view.GraphView;

public class MouseDrowPolygonOp implements IMouseOperator {

    private Polygon polygon = new Polygon();
    private GraphView mGraphView;

    public MouseDrowPolygonOp(GraphView mGraphView){
        this.mGraphView = mGraphView;
    }

    @Override
    public void mouseClick(MouseEvent event) {
    }

    @Override
    public void mouseReleased(MouseEvent event) {
    }

    @Override
    public void mouseEntered(MouseEvent event) {
    }


    /**
     * @param event 鼠标按下获取起始点坐标
     */
    @Override
    public void mousePressed(MouseEvent event) {
    	 if ( event.getButton() == MouseEvent.BUTTON1 ) {
             polygon.getPoints().add(event.getPoint());
             if (polygon.getHandleCount() == 1){
                 polygon.getPoints().add(event.getPoint());
             }
         }else {
             Polygon mPolygon = new Polygon(polygon);
             mGraphView.mGraphs.add(mPolygon);
             polygon.getPoints().clear();
             mGraphView.repaint();
             try {
 				if (GraphicalUtil.saveGraphInfo("save",null,MapFrame.canvas.mGraphs.get(0)) == null) {
 					JOptionPane.showMessageDialog(null, "自动保存多边形失败，请你先登录", "出错啦", JOptionPane.ERROR_MESSAGE);
 				}
 			} catch (IOException e) {
 				e.printStackTrace();
 			}
         }
    }

    @Override
    public void mouseExited(MouseEvent event) {
    }

    @Override
    public void mouseDragged(MouseEvent event) {
    }

    @Override
    public void mouseMoved(MouseEvent event) {
    	if ( polygon.getHandleCount() == 0 ) {
            return;
        }
    	polygon.draw(mGraphView.getGraphics());
    	polygon.getLastPoint().setLocation(event.getPoint());
        mGraphView.refresh();
        polygon.draw(mGraphView.getGraphics());
    }
}
